﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class BoKichDienDAO
    {
        // LOADBOKICHDIEN
        public DataTable LoadBoKichDien()
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADBOKICHDIEN", _cn);
            _da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            return _dt;
        }

        public void THEMBOKICHDIEN(BoKichDienDTO bkd)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("THEMBOKICHDIEN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@NgayNhap", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@MaTB", SqlDbType.Char, 20));
            cmd.Parameters.Add(new SqlParameter("@TenTB", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@Vol", SqlDbType.Float));
            cmd.Parameters.Add(new SqlParameter("@GhiChu", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@CSKhaDung", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@DonGia", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@MaLoai", SqlDbType.Int));


            cmd.Parameters["@NgayNhap"].Value = bkd.NGAYNHAP;
            cmd.Parameters["@MaTB"].Value = bkd.MATB;
            cmd.Parameters["@TenTB"].Value = bkd.TENTB;
            cmd.Parameters["@Vol"].Value = bkd.VOL;
            cmd.Parameters["@GhiChu"].Value = bkd.GHICHU;
            cmd.Parameters["@CSKhaDung"].Value = bkd.CSKHADUNG;
            cmd.Parameters["@DonGia"].Value = bkd.DONGIA;
            cmd.Parameters["@MaLoai"].Value = bkd.MALOAI;
            //int _kq = cmd.ExecuteNonQuery();
            cmd.ExecuteNonQuery();
            _cn.Close();
            // return _kq;
        }

        public int SuaBoKichDien(BoKichDienDTO bkd)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("SUABOKICHDIEN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@STT", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@NgayNhap", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("@MaTB", SqlDbType.Char, 20));
            cmd.Parameters.Add(new SqlParameter("@TenTB", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@CSKhaDung", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@DonGia", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@MaLoai", SqlDbType.Int));

            cmd.Parameters["@STT"].Value = bkd.STT;
            cmd.Parameters["@NgayNhap"].Value = bkd.NGAYNHAP;
            cmd.Parameters["@MaTB"].Value = bkd.MATB;
            cmd.Parameters["@TenTB"].Value = bkd.TENTB;
            cmd.Parameters["@CSKhaDung"].Value = bkd.CSKHADUNG;
            cmd.Parameters["@DonGia"].Value = bkd.DONGIA;
            cmd.Parameters["@MaLoai"].Value = bkd.MALOAI;

            int _kq = cmd.ExecuteNonQuery();
            //cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }

        public int XoaBoKichDien(int STT)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XOABOKICHDIEN", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@STT", SqlDbType.Int));
            cmd.Parameters["@STT"].Value = STT;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
    }
}
